package com.tianqu.tms.www.car.action;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.VehicleBrand;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.car.actionform.CarCreationInputActionForm;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;
/**
 * 用于新增入库跳转Action
 * 
 * @author Administrator
 *
 */

public class CarCreationDispatchAction extends TmsAction {
	private static final String LOADIMG = "imgLoad";//跳转到图片上传
	private static final String TOCONFIRM = "toConfirm";//保存确认
	private static final String IMGKEY = "imgKey";// 图片位置
	private static final String IMGTYPE = "imgType";// 图片类型
	private static final String IMGLIST = "imgList";//用于存放图片对象的list
	//private static final String FINDCAR = "findCar";//根据车牌号查询信息
	private static final String GOBACK ="goBack";
	private static final String VALIDATElICENSEPLATE = "validateLicensePlate";
	
	public int backType = 0;

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		CarCreationInputActionForm inputForm = (CarCreationInputActionForm)form;//获取表单信息，将表单信息放到viewhelp中
		

		CarCreationInputViewHelper viewhelp =(CarCreationInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		CarCreationInputViewHelper viewhelper = new CarCreationInputViewHelper();
		if(viewhelp != null){
			viewhelper = viewhelp;
		}
			
		
		String forwardPath = null;
		//modify by huangheli at 20150818 end for VIN码搜索功能删除 start
//		if(inputForm.getGetCarInfoButton() != null && !"".equals(inputForm.getGetCarInfoButton())){
//			forwardPath = FINDCAR;
//		}else{
		//modify by huangheli at 20150818 end for VIN码搜索功能删除 end
			//给ViewHelper赋值
			viewhelp = setViewHelperByForm(viewhelper,inputForm);	
//		}
		if(inputForm.getImgUpLoad1()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "1");
			forwardPath = LOADIMG;
		}
		if(inputForm.getImgUpLoad2()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "2");
			forwardPath = LOADIMG;
		}
		if(inputForm.getImgUpLoad3()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "3");
			forwardPath = LOADIMG;
		}
		if(inputForm.getImgUpLoad4()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "4");
			forwardPath = LOADIMG;
		}
		if(inputForm.getImgUpLoad5()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "5");
			forwardPath = LOADIMG;
		}
		if(inputForm.getImgUpLoad6()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "6");
			forwardPath = LOADIMG;
		}
		if(inputForm.getImgUpLoad7()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "7");
			forwardPath = LOADIMG;
		}
		if(inputForm.getImgUpLoad8()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "8");
			forwardPath = LOADIMG;
		}
		//跳转到确认页
		if(inputForm.getSaveInputInfo()!=null && !"".equals(inputForm.getSaveInputInfo()))
		{
			forwardPath = TOCONFIRM;
		}
		if(inputForm.getSaveInnerInfo()!=null && !"".equals(inputForm.getSaveInnerInfo()))
		{
			forwardPath = "toInnerResult";
		//modify by huangheli at 20150818 end for VIN码搜索功能删除 start
//		}else if((inputForm.getGetCarInfoButton() == null || "".equals(inputForm.getGetCarInfoButton())) && 
//				(viewhelp.getSerial() == null || "".equals(viewhelp.getSerial())) ){
		}else if((viewhelp.getSerial() == null || "".equals(viewhelp.getSerial())) ){
		//modify by huangheli at 20150818 end for VIN码搜索功能删除 end
			viewhelp.setCardType(String.valueOf(inputForm.getCardType()));
			viewhelp.setMemberId(String.valueOf(inputForm.getMemberId()));
			viewhelp.setCarScore(inputForm.getCarScore());
		}
		if(inputForm.getGetGoBackButton()!=null && !"".equals(inputForm.getGetGoBackButton()))
		{
			forwardPath = GOBACK;
		}
		if(!StringUtil.isNullOrBlank(inputForm.getValidateLicensePlate())){
			forwardPath = VALIDATElICENSEPLATE;
		}
		uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);	
		
		return mapping.findForward(forwardPath);
	}
	/**
	 * 把form的值赋给viewHelper
	 * @param viewHelper
	 * @param form
	 * @throws TmsDaoException 
	 */
	@SuppressWarnings("unchecked")
	private CarCreationInputViewHelper setViewHelperByForm(CarCreationInputViewHelper viewHelper,CarCreationInputActionForm form) throws TmsDaoException{
		CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
		viewHelper.setFromType(form.getFromType());
		
		//直接将页面获取的值set到viewhelp中  不需要进行处理
		if(!StringUtil.isNullOrBlank(form.getAttribute())){
			viewHelper.setAttribute(String.valueOf(form.getAttribute()));
		}else{
			viewHelper.setAttribute("");
		}	
		
		if(form.getLicensePlate() != null && !"".equals(form.getLicensePlate()) ){
			viewHelper.setLicensePlate(form.getLicensePlate());
		}
		if(form.getVin() != null && !"".equals(form.getVin())){
			viewHelper.setVin(form.getVin());
		}
		//图片部分赋值
	
		viewHelper.setCarOwner(form.getCarOwner());
		viewHelper.setCarOwnerIdCardNo(form.getCarOwnerIdCardNo());
		viewHelper.setCarType(String.valueOf(form.getCarType()));
		viewHelper.setCarUse(String.valueOf(form.getCarUse()));
		viewHelper.setColor(String.valueOf(form.getColor()));
		viewHelper.setCompanyContactPerson(form.getCompanyContactPerson());
		viewHelper.setCompanyContactPhone(form.getCompanyContactPhone());
		
		viewHelper.setEngine(form.getEngine());
		viewHelper.setFuelType(String.valueOf(form.getFuelType()));
		
		viewHelper.setJqx(form.getJqx());
		viewHelper.setRemark(form.getRemark());//WL:20150509
		viewHelper.setAttestStatus(form.getAttestStatus());
		viewHelper.setLoadquality(form.getLoadquality());
		
		//车辆名称取值
		VehicleBrand brand = VehicleBrand.getInstance();
		String brandIdStr = "";
		if(StringUtil.isNullOrBlank(form.getCarBrand())){
			brandIdStr = form.getBrandId();
		}else{
			brandIdStr = form.getCarBrand();
		}
		
		if (StringUtil.isNullOrBlank(form.getCarBrand()) && StringUtil.isNullOrBlank(form.getBrandId())) {
			viewHelper.setCarName(" ");
			viewHelper.setGearbox("");
			viewHelper.setCountryStyle("");
		} else {
			List list = new ArrayList();
			if(form.getFromType() == 1){
				list = brand.getAllVehicleBrandInfo(form.getVendorId());
			}else{
				list = commercialVehicle.getAllVehicleBrandInfo(form.getVendorId());
			}
			
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					Map map = (Map) list.get(i);
					if (map.get("brandId").equals(brandIdStr)) {
						String carName = (String) map.get("brandName");
						viewHelper.setCarName(carName);
					}
				}
			}else{
				viewHelper.setCarName(" ");
			}
			viewHelper.setGearbox(String.valueOf(form.getGearbox()));
			viewHelper.setCountryStyle(String.valueOf(form.getCountryStyle()));
		}

		if("0".equals(form.getMileage())){
			viewHelper.setMileage("");
		}else{
			viewHelper.setMileage(form.getMileage());
		}
		
		viewHelper.setModelCode(form.getModelCode());
		viewHelper.setOwnerAddress(form.getOwnerAddress());
		viewHelper.setOwnerCardNo(form.getOwnerCardNo());
		viewHelper.setOwnerOrgNo(form.getOwnerOrgNo());
		viewHelper.setOwnerPhone(form.getOwnerPhone());
		if(!StringUtil.isNullOrBlank(form.getOwnerType())){
			viewHelper.setOwnerType(String.valueOf(form.getOwnerType()));
		}
		viewHelper.setPassenger(form.getPassenger());
		
		
		if("0".equals(form.getPrice())){
			viewHelper.setPrice("");
		}else{
			viewHelper.setPrice(form.getPrice());
		}
		viewHelper.setRegDate(form.getRegDate());
		viewHelper.setRegisterCardNo(form.getRegisterCardNo());
		viewHelper.setSendCardDate(form.getSendCardDate());
		viewHelper.setUpdateTime(form.getUpdateTime());//--------------------
		viewHelper.setVendorCode(form.getVendorCode());
		viewHelper.setYearCarefulPeriod(form.getYearCarefulPeriod());
		viewHelper.setCarStyle(form.getCarStyle());
		viewHelper.setCardType(form.getCardType());
		Codes code = Codes.getInstance();
		
		viewHelper.setCarTypeList(code.getCodeslistByItemId(Codes.VEHICLE_TYPE));//车辆类型
		viewHelper.setColorList(code.getCodeslistByItemId(Codes.VEHICLE_COLOR));//车辆颜色
		viewHelper.setCarUseList(code.getCodeslistByItemId(Codes.USER_NATURE));// 使用性质list
		viewHelper.setCardTypeList(code.getCodeslistByItemId(Codes.PLATE_TYPE));// 号牌种类list
		viewHelper.setFuelTypeList(code.getCodeslistByItemId(Codes.FUEL_TYPE));// 燃料种类list
		viewHelper.setCountryStyleList(code.getCodeslistByItemId(Codes.VEHICLE_COUNTRY));// 国别list
		viewHelper.setAttributeList(code.getCodeslistByItemId(Codes.ATTACHMENT_PROPERTY));// 车辆状态list' 
		viewHelper.setOwnerTypeList(code.getCodeslistByItemId(Codes.CAR_OWNER));// 车主类型list
		viewHelper.setGearBoxList(code.getCodeslistByItemId(Codes.GEARBOX_TYPE));//变速箱类型list
		
		Member menber = Member.getInstance();
		viewHelper.setMemberList(menber.findAllMemInfo());// 会员类型
		if("1".equals(viewHelper.getSearchFlag())){
			viewHelper.setMemberId(form.getMemberId());
		}

		//车辆品牌
		Vendor vendor = Vendor.getInstance();
		CarStyle carStyle = CarStyle.getInstance();
		
		if(form.getFromType() == 1){
			viewHelper.setVendorList(vendor.findAllVendorInfo());
			viewHelper.setCarStyleList(carStyle.getAllCarStyleInfo(form.getBrandId()));
			viewHelper.setBrandList(brand.getAllVehicleBrandInfo(form.getVendorId()));	
		}else{
			viewHelper.setVendorList(commercialVehicle.findAllVendorInfo());
			viewHelper.setCarStyleList(commercialVehicle.getAllStyleInfo(form.getBrandId()));
			viewHelper.setBrandList(commercialVehicle.getAllVehicleBrandInfo(form.getVendorId()));
		}
		

		viewHelper.setBrandId(form.getBrandId());
		viewHelper.setVendorId(form.getVendorId());
		
		
		viewHelper.setDisplacement(form.getDisplacement());	
	
		if(!"".equals(form.getCarStyle()) && form.getCarStyle() != null){
			viewHelper.setCarStyleId(form.getCarStyle());
		}else{
			viewHelper.setCarStyleId(form.getCarStyleId());
		}
		
		
		
		
		return viewHelper;
	}
	public int getBackType() {
		return backType;
	}
	public void setBackType(int backType) {
		this.backType = backType;
	}
	
	 

}
